package cn.ailearn.module.controller.common;

import cn.ailearn.module.pojo.dto.AccountDto;
import cn.ailearn.module.service.UsersService;
import cn.ailearn.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName LoginController
 * @Description 用户控制器
 * @Author WDEMO
 * @Date 2025/10/23 14:26
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/common/user")
@Tag(name = "用户相关接口",description = "用户相关的操作")
public class UserController {

    private final UsersService usersService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<String> login(@RequestBody AccountDto accountDto){
        String token = usersService.login(accountDto.getUsername(), accountDto.getPassword());
        return Result.success(token);
    }


    @PostMapping("/logout")
    @Operation(summary = "用户退出登录")
    public Result loginOut(){
        return  usersService.loginOut() ? Result.success("退出登录成功") : Result.error("退出登录失败");

    }

    @PostMapping("/testPre")
    @Operation(summary = "测试权限接口")
    @PreAuthorize("hasAuthority('user:vip')")
    public Result testPre(){
        return Result.success("测试权限接口成功");
    }
}
